package com.aidex.system.service.impl;

import com.aidex.common.core.service.impl.BasicServiceImpl;
import com.aidex.common.utils.AliOssUtils;
import com.aidex.system.domain.SysOss;
import com.aidex.system.mapper.SysOssMapper;
import com.aidex.system.service.SysOssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
@Transactional(readOnly = true)
public class SysOssServiceImpl extends BasicServiceImpl<SysOssMapper, SysOss> implements SysOssService {

    @Autowired
    private AliOssUtils aliOssUtils;

    @Override
    @Transactional
    public void upload(MultipartFile multipartFile) throws IOException {
        String fileName = multipartFile.getOriginalFilename();
        fileName = aliOssUtils.getFileName(fileName);
        SysOss oss = new SysOss();
        multipartFile.getSize();
        oss.setFileName(fileName);
        String url = aliOssUtils.upload(multipartFile, "upload");
        oss.setSize(multipartFile.getSize());
        oss.setContentType(multipartFile.getContentType());
        // 返回阿里云原生域名前缀URL
        oss.setUrl(aliOssUtils.getOriginalUrl(url));
        this.insert(oss);
    }

}
